// pages/active/active.js
import WxValidate from '../../utils/WxValidate'
import {
  bindCard,
  getCardType
} from '../../apis/api'
// import { ColorGradient } from 'XrFrame/components/particle/gradient'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeCardForm: {
      cardAccount: '',
      password: '',
      addressDTO: {
        receiver: '',
        address: '',
        telephone: '',
      },
      contactTelephone: '',
      deliveryTime: '',
      isTwo: 0,
      week: 1,
      weeks: {
        aWeek: 1,
        bWeek: 4
      }
    },
    num: 0,
    city: false,
    oneFlag: true,
    isOne: 0,
    region: ['', '', ''],
    date: '',
    dateV: '',
    dateRange: ['每周一', '每周二', '每周三', '每周四', '每周五', '每周六', '每周日'],
    dateRangeV: ['每周一,每周四', '每周二,每周五', '每周三,每周六', '每周四,每周日'],
    addCard: [],
    addPassword: []
  },

  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value,
      city: true
    })
    wx.hideKeyboard()
  },

  bindPickerChange: function (e) {
    this.setData({
      date: e.detail.value,
      ['activeCardForm.deliveryTime']: this.data.dateRange[e.detail.value]
    })
    wx.hideKeyboard()
  },

  bindPickerChangeV: function (e) {
    this.setData({
      dateV: e.detail.value,
      ['activeCardForm.deliveryTime']: this.data.dateRangeV[e.detail.value]
    })
  },

  bindCardChange: function (e) {
    getCardType({
      cardAccount: e.detail.value
    }).then(res => {
      console.log(res)
      this.setData({
        oneFlag: true
      })
      if (res.data !== null && res.data.type === "至尊VIP会员卡") {
        this.setData({
          ['activeCardForm.isTwo']: 1
        })
      } else {
        this.setData({
          ['activeCardForm.isTwo']: 0
        })
        if (res.data.type == "10斤大米礼盒装兑换卡" || res.data.type == "20斤大米提货卡" || res.data.type == "两无化蔬菜单次卡A" || res.data.type == "两无化蔬菜单次卡B"|| res.data.type == "荤素礼品月卡B"|| res.data.type == "蔬菜礼品月卡A"|| res.data.type == "电信劳模尊享套餐卡") {
          console.log(res.data.type)
          this.setData({
            oneFlag: false,
            isOne: 1
          })
        }
      }

    })

    this.setData({
      ['activeCardForm.cardAccount']: e.detail.value
    })
    return e.detail.value
  },
  addCard() {
    let number = this.data.num + 1
    this.setData({
      num: number
    })
  },
  bindAddCardChange(e) {
    // console.log(e)
    let num = e.target.dataset.num
    this.setData({
      [`addCard[${num-1}]`]: e.detail.value,
    })
  },
  bindAddPasswordChange(e) {
    // console.log(e)
    let num = e.target.dataset.num
    this.setData({
      [`addPassword[${num-1}]`]: e.detail.value,
    })
  },
  bindPasswordChange: function (e) {
    this.setData({
      ['activeCardForm.password']: e.detail.value,
    })
  },
  bindUsernameChange: function (e) {
    this.setData({
      ['activeCardForm.addressDTO.receiver']: e.detail.value
    })
  },
  bindTelephoneChange: function (e) {
    this.setData({
      ['activeCardForm.addressDTO.telephone']: e.detail.value,
      ['activeCardForm.contactTelephone']: e.detail.value,
      ['activeCardForm.addressDTO.telephone']: e.detail.value
    })
  },

  bindAddressChange: function (e) {
    const address = this.data.region.join('');
    this.setData({
      ['activeCardForm.addressDTO.address']: address + e.detail.value
    })
  },

  activeCard: async function () {
    // if (!this.data.activeCardForm.isTwo) {
    //   delete this.data.activeCardForm.weeks
    //   switch (this.data.activeCardForm.deliveryTime) {
    //     case '每周一':
    //       this.setData({
    //         ['activeCardForm.week']: 1
    //       })
    //       break;
    //     case '每周二':
    //       this.setData({
    //         ['activeCardForm.week']: 2
    //       })
    //       break;
    //     case '每周三':
    //       this.setData({
    //         ['activeCardForm.week']: 3
    //       })
    //       break;
    //     case '每周四':
    //       this.setData({
    //         ['activeCardForm.week']: 4
    //       })
    //       break;
    //     case '每周五':
    //       this.setData({
    //         ['activeCardForm.week']: 5
    //       })
    //       break;
    //     case '每周六':
    //       this.setData({
    //         ['activeCardForm.week']: 6
    //       })
    //       break;
    //     case '每周日':
    //       this.setData({
    //         ['activeCardForm.week']: 7
    //       })
    //       break;
    //   }
    // } else {
    //   switch (this.data.activeCardForm.deliveryTime) {
    //     case '每周一,每周四':
    //       this.setData({
    //         ['activeCardForm.weeks.aWeek']: 1,
    //         ['activeCardForm.weeks.bWeek']: 4
    //       })
    //       break;
    //     case '每周二,每周五':
    //       this.setData({
    //         ['activeCardForm.weeks.aWeek']: 2,
    //         ['activeCardForm.weeks.bWeek']: 5
    //       })
    //       break;
    //     case '每周三,每周六':
    //       this.setData({
    //         ['activeCardForm.weeks.aWeek']: 3,
    //         ['activeCardForm.weeks.bWeek']: 6
    //       })
    //       break;
    //     case '每周四,每周日':
    //       this.setData({
    //         ['activeCardForm.weeks.aWeek']: 4,
    //         ['activeCardForm.weeks.bWeek']: 7
    //       })
    //       break;
    //   }
    // }
    // delete this.data.activeCardForm.deliveryTime
    // console.log('reactiveCardForms', this.data.activeCardForm);
    // const res = await bindCard(this.data.activeCardForm)
    // console.log('res', res);
    // if (res.code !== 200) {
    //   return wx.showModal({
    //     content: res.msg,
    //     showCancel: false
    //   })
    // }
    // wx.showToast({
    //   title: '激活成功',
    //   icon: 'success',
    //   duration: 1000
    // })
    // setTimeout(() => {
    //   wx.switchTab({
    //     url: '/pages/myvip/myvip',
    //   })
    // }, 1000)

  },
  /**
   * 生命周期函数--监听页面加载
   */
  // 校验
  showModal(err) {
    wx.showModal({
      content: err.msg,
      showCancel: false
    })
  },
  initValidate() {
    const rules = {
      cardId: {
        required: true
      },
      password: {
        required: true
      },
      nickname: {
        required: true
      },
      telephone: {
        required: true,
        tel: true
      },
      regionone: {
        required: true
      },
      regiontwo: {
        required: true
      },
      regionthree: {
        required: true
      },
      address: {
        required: true
      },
      date: {
        required: true
      },
    }
    const messages = {
      telephone: {
        required: '请输入手机号',
        tel: '请输入正确的手机号'
      },
      password: {
        required: '请输入密码'
      },
      cardId: {
        required: '请输入卡号'
      },
      nickname: {
        required: '请输入收货人'
      },
      regionone: {
        required: '请选择省份'
      },
      regiontwo: {
        required: '请选择省份'
      },
      regionthree: {
        required: '请选择省份'
      },
      address: {
        required: '请输入地址'
      },
      date: {
        required: '请选择配送日期'
      },
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  formSubmit: async function (e) {
    const params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      if (this.data.isOne == 1 && this.WxValidate.errorList.length == 1) {
        delete this.data.activeCardForm.weeks
        // delete this.data.activeCardForm.week
        // delete this.data.activeCardForm.isTwo
        // console.log(this.data.activeCardForm)
        delete this.data.activeCardForm.deliveryTime
        console.log(this.data.num)
        const res = await bindCard(this.data.activeCardForm)
        if (res.code == 200) {
          if (this.data.num != 0) {
            let form = this.data.activeCardForm
            for (let i = 0; i < this.data.addCard.length; i++) {
              form.cardAccount = this.data.addCard[i]
              form.password = this.data.addPassword[i]
              const res = await bindCard(form)
              if (res.code !== 200) {
                return wx.showModal({
                  content: res.msg,
                  showCancel: false
                })
              }
            }
          }
          wx.showToast({
            title: '激活成功',
            icon: 'success',
            duration: 1000
          })
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/myvip/myvip',
            })
          }, 1000)
        } else {
          return wx.showModal({
            content: res.msg,
            showCancel: false
          })
        }

      } else {
        this.showModal(error)
        if (!this.data.activeCardForm.isTwo) {
          delete this.data.activeCardForm.weeks
          switch (this.data.activeCardForm.deliveryTime) {
            case '每周一':
              this.setData({
                ['activeCardForm.week']: 2
              })
              break;
            case '每周二':
              this.setData({
                ['activeCardForm.week']: 3
              })
              break;
            case '每周三':
              this.setData({
                ['activeCardForm.week']: 4
              })
              break;
            case '每周四':
              this.setData({
                ['activeCardForm.week']: 5
              })
              break;
            case '每周五':
              this.setData({
                ['activeCardForm.week']: 6
              })
              break;
            case '每周六':
              this.setData({
                ['activeCardForm.week']: 7
              })
              break;
            case '每周日':
              this.setData({
                ['activeCardForm.week']: 1
              })
              break;
          }
        } else {
          switch (this.data.activeCardForm.deliveryTime) {
            case '每周一,每周四':
              this.setData({
                ['activeCardForm.weeks.aWeek']: 2,
                ['activeCardForm.weeks.bWeek']: 5
              })
              break;
            case '每周二,每周五':
              this.setData({
                ['activeCardForm.weeks.aWeek']: 3,
                ['activeCardForm.weeks.bWeek']: 6
              })
              break;
            case '每周三,每周六':
              this.setData({
                ['activeCardForm.weeks.aWeek']: 4,
                ['activeCardForm.weeks.bWeek']: 7
              })
              break;
            case '每周四,每周日':
              this.setData({
                ['activeCardForm.weeks.aWeek']: 5,
                ['activeCardForm.weeks.bWeek']: 1
              })
              break;
          }
        }
      }
      return false
    }

    // console.log(this.data.activeCardForm)
    delete this.data.activeCardForm.deliveryTime
    // console.log(this.data.activeCardForm)
    console.log(this.data.num)
    const res = await bindCard(this.data.activeCardForm)
    if (res.code === 200) {
      if (this.data.num != 0) {
        let form = this.data.activeCardForm
        for (let i = 0; i < this.data.addCard.length; i++) {
          form.cardAccount = this.data.addCard[i]
          form.password = this.data.addPassword[i]
          const res = await bindCard(form)
          if (res.code !== 200) {
            return wx.showModal({
              content: res.msg,
              showCancel: false
            })
          }
        }
      }

    } else {
      return wx.showModal({
        content: res.msg,
        showCancel: false
      })
    }
    wx.showToast({
      title: '激活成功',
      icon: 'success',
      duration: 1000
    })
    setTimeout(() => {
      wx.switchTab({
        url: '/pages/myvip/myvip',
      })
    }, 1000)
  },
  onLoad(options) {
    this.initValidate()
  }
})